<template>
    <div class="wrapper">
        <van-tabs title-active-color="#FF2828" color="#FF2828" @click="tabDitch()" v-model:active="active">
            <van-tab :title="item.channelName" v-for="(item,index) in tabList" :key="index" :name="index" />
        </van-tabs>
        <!-- <div class="dis">
			此功能暂未开放
		</div> -->
        <div class="dis" v-for="(item,index) in list" :key="index">
            <div class="main" :style="{backgroundImage:'url('+item.bacImg+')'}">
                <div class="dia" style="justify-content: space-between;">
                    <div class="" v-if="item.accountType==0">
                        分润账户
                    </div>
                    <div class="" v-else-if="item.accountType==1">
                        返现账户
                    </div>
                    <div class="" v-else>
                        服务费账户
                    </div>
                    <div class="inquire" :style="{color:item.bacColor}" @click="history(item)">
                        历史账户查询
                    </div>
                </div>
                <div class="dia withdraw_box" style="justify-content: space-between;">
                    <div class="main_price">
                        {{item.accountBalance}}
                    </div>
                    <div class="dis withdraw" :style="{color:item.bacColor}" @click="submit(item)">
                        提现
                    </div>
                </div>
            </div>
        </div>
        <div class="btn dis" v-if="agentMerge=='Y'&&(yzxSignStatus==1||yzxSignStatus=='1')">
            <div class="btn_box dis" @click="merge">
                合并提现
            </div>
        </div>
        <div class="btn dia signBtn" style="width: 100%;justify-content: space-between;"
            v-if="yzxSignStatus == 0 || yzxSignStatus == '0'">
            <div class="btn_box dis contractBox" @click="getyzxSigning">
                去签约
            </div>
            <div class="btn_box dis contractBox endorse" @click="selectSign">
                已签约？刷新签约状态
            </div>
        </div>
    </div>
</template>

<script>
    import {
        selectAgentChannelInf,
        selectAgentAccount,
        selectAgencyConfig,
        agentYhsSign,
        agentYzxSign,
        updateAgentYzxSignStatus
    } from '@/api/terminal'
    import store from '@/store'
    import {
        showToast,
        showConfirmDialog
    } from 'vant'

    export default defineComponent({
        name: 'sateActive',
        setup() {
            const router = useRouter()
            const state = reactive({
                tabList: [],
                channelCode: "",
                active: 0,
                list: [],
                agentMerge: "",
                yhsSignStatus: "",
                yzxSignStatus: "",
                taxChannel: ""
            })
            //查询云智鑫签约状态
            const selectSign = () => {
                let prames = {

                }
                updateAgentYzxSignStatus(prames).then(res => {
                    showToast(res.message)
                    getFindPageList()
                    getselectAgencyConfig()
                })
            }
            //跳转云智鑫签约地址
            const getyzxSigning = () => {
                let parames = {

                }
                agentYzxSign(parames).then(res => {
                    console.log(res)
                    showToast(res.message)
                    // window.location.href = res.data.message
                })
            }
            //历史账户查询
            const history = (e) => {
                router.push({
                    path: "/historyAccount",
                    query: {
                        channelCode: state.channelCode,
                        accountType: e.accountType
                    }
                })
            }
            //获取签约地址
            const getSigning = () => {
                let parames = {
                }
                agentYhsSign(parames).then(res => {
                    console.log(res)
                    window.location.href = res.data.message

                })
            }
            //跳转提现
            const submit = (item) => {
                if (state.taxChannel == 'yhs') {
                    if (state.yhsSignStatus == 0 || state.yhsSignStatus == '0') {
                        showConfirmDialog({
                                title: '提示',
                                message: '税源未签约,请先签约!',
                            })
                            .then(() => {
                                // on confirm
                                getSigning()
                            })
                            .catch(() => {
                                // on cancel
                            });
                    } else {
                        router.push({
                            path: "/withdraw",
                            query: {
                                accountType: item.accountType,
                                channelCode: state.channelCode,
                                accountBalance: item.accountBalance
                            }
                        })
                    }
                } else {
                    if (state.yzxSignStatus == 0 || state.yzxSignStatus == '0') {
                        showToast('税源未签约,请先点击签约!')
                        // showConfirmDialog({
                        //         title: '提示',
                        //         message: '税源未签约,请先签约!',
                        //     })
                        //     .then(() => {
                        //         // on confirm
                        //         getyzxSigning()
                        //     })
                        //     .catch(() => {
                        //         // on cancel
                        //     });
                    } else {
                        router.push({
                            path: "/withdraw",
                            query: {
                                accountType: item.accountType,
                                channelCode: state.channelCode,
                                accountBalance: item.accountBalance
                            }
                        })
                    }
                }

            }
            //获取合并提现开关
            const getselectAgencyConfig = () => {
                selectAgencyConfig({
                    channelCode: state.channelCode
                }).then(res => {
                    state.agentMerge = res.data.agentMerge
                })
            }
            //获取渠道列表
            const getTabList = () => {
                selectAgentChannelInf({
                    channelType: 1
                }).then(res => {
                    state.tabList = res.data
                    state.channelCode = state.tabList[state.active].channelCode
                    getselectAgencyConfig()
                    getFindPageList()
                })
            }
            getTabList()
            //tab切换
            const tabDitch = () => {
                state.channelCode = state.tabList[state.active].channelCode
                state.pageStart = 1
                state.list = []
                getFindPageList()
            }
            //查询账户
            const getFindPageList = () => {
                selectAgentAccount({
                    channelCode: state.channelCode,
                }).then(res => {
                    state.list = res.data.agentAcountList
                    state.yhsSignStatus = res.data.yhsSignStatus
                    state.taxChannel = res.data.taxChannel
                    state.yzxSignStatus = res.data.yzxSignStatus
                    for (let i in state.list) {
                        state.list[i]['bacImg'] = `http://agent.mtpospay.com/ac${i}.png`
                        if (i == 0) {
                            state.list[i]['bacColor'] = '#FFB93B'
                        } else if (i == 1) {
                            state.list[i]['bacColor'] = '#5FC457'
                        } else if (i == 2) {
                            state.list[i]['bacColor'] = '#7B66E4'
                        } else if (i == 3) {
                            state.list[i]['bacColor'] = '#3dc8e6'
                        }
                    }
                })
            }
            //跳转合并提现
            const merge = () => {
                if (state.yhsSignStatus == 0 || state.yhsSignStatus == '0') {
                    showConfirmDialog({
                            title: '提示',
                            message: '税源未签约,请先签约!',
                        })
                        .then(() => {
                            // on confirm
                            getSigning()
                        })
                        .catch(() => {
                            // on cancel
                        });
                } else {
                    router.push({
                        path: "/combine",
                        query: {
                            channelCode: state.channelCode,
                            // accountType: e.accountType
                        }
                    })
                }

            }
            return {
                ...toRefs(state),
                getTabList,
                tabDitch,
                getFindPageList,
                submit,
                history,
                merge,
                getselectAgencyConfig,
                getSigning,
                getyzxSigning,
                selectSign
            }
        }
    })
</script>
<style scoped lang="scss">
    .wrapper {
        background-color: #fff;
        width: 100%;
        height: 100%;

        // height: 100%;
        min-height: 100vh;

        .main {
            margin-top: 12px;
            width: 92%;
            // height: 120px;
            background-image: url('../../assets/terminal/purseBg1.png');
            background-size: 100% 100%;
            background-repeat: no-repeat;
            padding: 14px 14px;
            box-sizing: border-box;
            font-size: 14px;
            color: #fff;

            .inquire {
                font-size: 14px;
            }

            .withdraw_box {
                margin-top: 24px;

                .main_price {}

                .withdraw {
                    // width: 54px;
                    // height: 20px;
                    padding: 4px 16px;
                    background: #FFFFFF;
                    border-radius: 34px;
                    color: #FFB93B;
                    font-size: 12px;

                }
            }

        }

        .btn {
            position: absolute;
            bottom: 10%;
            left: 50%;
            transform: translateX(-50%);

            .btn_box {
                width: 339px;
                height: 40px;
                background: #ff2828;
                border-radius: 6px 6px 6px 6px;
                font-family: PingFang SC, PingFang SC;
                font-weight: 400;
                font-size: 14px;
                color: #FFFFFF;
            }
        }

        .signBtn {
            padding: 0px 30px;
            box-sizing: border-box;
            bottom: 10%;

            .contractBox {
                width: 140px;
            }

            .endorse {
                width: 140px;
                height: 30px;
                background: #CCCCCC;
                font-size: 12px;
            }
        }
    }
</style>